package com.caipiao.crawler.shengfucang;

import com.alibaba.fastjson.JSON;
import com.caipiao.crawler.mapper.LotteryCurrentMapper;
import com.caipiao.crawler.mapper.LotteryNumberMapper;
import com.caipiao.crawler.mapper.LotteryTicketMapper;
import com.caipiao.crawler.model.DaletouResultModel;
import com.caipiao.crawler.model.LotteryNumber;
import com.caipiao.crawler.model.LotteryTicket;
import com.caipiao.crawler.model.Shengfc14ResultModel;
import com.caipiao.crawler.zhongjiangRule.ZhongJiangRule;
import com.caipiao.util.DBTools;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class ShengfucangResultPipeline implements Pipeline {

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        LotteryNumber m = resultItems.get("model");
        System.err.println("pipe process:" + Thread.currentThread().getId());
        if(m == null) {
            return;
        }
        commit(m);
    }

    public void commit(LotteryNumber model) {
        System.err.println("commit");
        System.err.println(JSON.toJSONString(model));
        //TODO 写数据库，需要判断不存在的情况才写入
        SqlSession session = DBTools.getSession();
        LotteryNumberMapper mapper = session.getMapper(LotteryNumberMapper.class);
        LotteryTicketMapper ticketMapper = session.getMapper(LotteryTicketMapper.class);
        LotteryCurrentMapper currentMapper = session.getMapper(LotteryCurrentMapper.class);
        Map map1 = new HashMap();
        map1.put("term",model.getTerm());
        map1.put("type","4");
        int count = mapper.selectCountByTerm(map1);
        if(count==0) {
            mapper.insertModel(model);
            map1.clear();
            map1.put("term",model.getTerm());
            map1.put("name","shengfu");
            map1.put("updated_time",(int) (System.currentTimeMillis()/1000L));
            currentMapper.updateCurrent(map1);
            session.commit();
        }
        int countSfc = mapper.selectCountByTermForSfc(map1);
        if(countSfc==1){
            mapper.updetaBymodelForSfcOrJqc(model);
            session.commit();
        }
        //TODO 如果第一次写入，触发更新判断是否中奖的逻辑
        //胜负场中奖校验
        if(countSfc==1&&model.getNumber()!=null){
            String prizeNumber = model.getNumber(); //中奖号码
            Map map = new HashMap<String,Object>();
            map.put("term",model.getTerm());
            map.put("type","6");
            map.put("status","1");
            List<LotteryTicket> ticketList = ticketMapper.selectByTermAndType(map);
            for(LotteryTicket lotteryTicket : ticketList){
                String number = lotteryTicket.getNumber();
                Map ticketMap = ZhongJiangRule.shengfuchangRule(number,model.getNumber(),model,lotteryTicket.getMultiple());
                ticketMap.put("id",lotteryTicket.getId());
                ticketMap.put("updated_time",(int) (System.currentTimeMillis()/1000L));
                ticketMapper.updateTicketById(ticketMap);
            }
            session.commit();
        }
        //任选9中奖校验
        if(countSfc==1&&model.getNumber()!=null){
            String prizeNumber = model.getNumber(); //中奖号码
            Map map = new HashMap<String,Object>();
            map.put("term",model.getTerm());
            map.put("type","7");
            List<LotteryTicket> ticketList = ticketMapper.selectByTermAndType(map);
            for(LotteryTicket lotteryTicket : ticketList){
                String number = lotteryTicket.getNumber();
                Map ticketMap = ZhongJiangRule.renxuan9Rule(number,model.getNumber(),model,lotteryTicket.getMultiple());
                ticketMap.put("id",lotteryTicket.getId());
                ticketMap.put("updated_time",(int) (System.currentTimeMillis()/1000L));
                ticketMapper.updateTicketById(ticketMap);
            }
            session.commit();
        }
        session.close();
    }

}
